<?php

require_once 'doctrine'.DIRECTORY_SEPARATOR.'Doctrine.php';
 
@include APPPATH . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'database' . EXT;

/*
*create a fixtures and schema directory in the application directory
*so it coincides with the directories defined below
*/
define('MODELS_DIRECTORY', APPPATH . DIRECTORY_SEPARATOR . 'models' . DIRECTORY_SEPARATOR);
define('FIXTURES_DIRECTORY', APPPATH . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR);
define('SCHEMA_DIRECTORY', APPPATH . DIRECTORY_SEPARATOR . 'schema' . DIRECTORY_SEPARATOR);


// Set the autoloader
spl_autoload_register(array('Doctrine', 'autoload'));

//optional, you can set this to whatever you want, or not set it at all
//Doctrine_Manager::getInstance()->setAttribute('model_loading', 'aggressive');

// Load the Doctrine connection
// (Notice the use of $active_group here, to make it easy to swap out
//  you connection based on you database.php configs)

if (!isset($db[$active_group]['dsn'])) {
	//try to create the dsn, if it has not been manually set
	//in your config. I personally would opt to set my
	//dsn manually, but it works either way
	$db[$active_group]['dsn'] = $db[$active_group]['dbdriver'] . 
                       '://' . $db[$active_group]['username'] . 
                       ':' . $db[$active_group]['password']. 
                       '@' . $db[$active_group]['hostname'] . 
                       '/' . $db[$active_group]['database'];
}

//	print_r($db[$active_group]['dsn']);
Doctrine_Manager::connection($db[$active_group]['dsn']);


function doctrine_load_models() {
	if(file_exists(MODELS_DIRECTORY . DIRECTORY_SEPARATOR . 'generated')) {
		Doctrine::loadModels(MODELS_DIRECTORY . DIRECTORY_SEPARATOR . 'generated');
		Doctrine::loadModels(MODELS_DIRECTORY);	
	}	
}
doctrine_load_models();

function doctrine_install() {
	doctrine_create_database();
	doctrine_populate_database();
	doctrine_load_models();
}

function doctrine_uninstall() {
	doctrine_destroy_database();
	doctrine_destroy_models();
}

function doctrine_reinstall() {
	doctrine_uninstall();
	doctrine_install();
}

function doctrine_create_database() {
	if(count(scandir(MODELS_DIRECTORY)) == 2) {
		doctrine_create_models();
	}
	
	Doctrine::createTablesFromModels(MODELS_DIRECTORY);	
}
function doctrine_populate_database() {	
	$data = new Doctrine_Data();
	$data->importData(FIXTURES_DIRECTORY);		
}
function doctrine_destroy_database() {
	$conn = Doctrine_Manager::getInstance()->getCurrentConnection();
	
	$models = Doctrine::getLoadedModels();	
	
	foreach($models as $model) {
		$conn->export->dropTable(Doctrine::getTable($model)->getTableName());
	}
}

function doctrine_create_models() {
	Doctrine::generateModelsFromYaml(SCHEMA_DIRECTORY, MODELS_DIRECTORY);
}
function doctrine_destroy_models() {
	foreach(scandir(MODELS_DIRECTORY) as $file) {
		if($file == '.' || $file == '..') {
			continue;
		}
		
		$file = MODELS_DIRECTORY . $file;
		if(is_dir($file)) {
			foreach(scandir($file) as $subfile) {
				if($subfile != '.' && $subfile != '..') {
					unlink($file . DIRECTORY_SEPARATOR . $subfile);
				}
			}
			rmdir($file);
		} else {
			unlink($file);
		}		
	}
}
?>